//
//  RankFirstLevelController.m
//  Kugou-OC
//
//  Created by BH on 16/6/15.
//  Copyright © 2016年 Sunny. All rights reserved.
//

#import "RankFirstLevelController.h"
#import "GlobalDefine.h"
#import "RankCell.h"
#import "RankModel.h"
#import "NetworkHelper.h"
#import "RankSecondLevelController.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import <SDAutoLayout/UIView+SDAutoLayout.h>

@interface RankFirstLevelController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic,strong)UITableView *tableView;
@property(nonatomic,strong)NSMutableArray *dataArray;

@end

@implementation RankFirstLevelController

- (void)viewDidLoad {
    [super viewDidLoad];
    //获取数据
    [self getData];
    
    self.title = self.rankModel.rankName;
    self.rightItems = @{@"img1":@"top_search",@"img2":@"top_list"};
}

-(UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-80-64) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
    }
    
    return _tableView;
}

-(NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = ALLOC(NSMutableArray);
    }
    
    return _dataArray;
}

/**
 *  获取数据
 */
-(void)getData{
    NSString *url = @"http://ioscdn.kugou.com/api/v3/rank/list";
    NSDictionary *params = @{@"plat":@"2",@"withsong":@"1",@"showtype":@"2",@"parentid":@"56"};
    
    [[NetworkHelper shareNetwork] getDataWithUrl:url params:params view:self.view success:^(id response) {
        NSArray *info = response[@"data"][@"info"];
        
        for (NSDictionary *dict in info) {
            RankModel *model = [[RankModel alloc] initWithDict:dict];
            [self.dataArray addObject:model];
        }
        
        if (!self.tableView) {
            [self.tableView reloadData];
        }else{
            [self.view addSubview:self.tableView];
        }
        
    } failure:^(NSError *error) {
        
    }];
}

#pragma mark - UITableView datasource & delegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.rankModel.children.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    RankCell *cell = [RankCell cellWithTableView:tableView];
    
    if (indexPath.row) {
        cell.indexPath = indexPath;
        cell.titleLab.text = [self.rankModel.children[indexPath.row-1] objectForKey:@"rankname"];
        cell.rankM = self.dataArray[indexPath.row-1];
    }else{
        UIImageView *headerImg = ALLOC(UIImageView);
        headerImg.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT*0.25);
        [headerImg sd_setImageWithURL:[NSURL URLWithString:self.rankModel.bannerurl] placeholderImage:IMAGE(@"placeholder_rectangle")];
        [cell.contentView addSubview:headerImg];
        
        cell.accessoryType = UITableViewCellAccessoryNone;
    }
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row) {
        return 110;
    }else{
        return SCREEN_HEIGHT*0.25;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    RankSecondLevelController *second = ALLOC(RankSecondLevelController);
    second.rankModel = self.dataArray[indexPath.row-1];
    PUSH(second);
}

@end
